<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false" style="min-width: 1400px;"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="物品名称：" prop="name">
        <el-input v-model="dataForm.name" placeholder="物品名称" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="宠物物品tag：" prop="tag">
        <el-select v-model="dataForm.tag">
          <el-option
            v-for="option in tagOptions"
            :key="option.id"
            :label="option.name"
            :value="option.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物品分类：" prop="propType">
        <el-select v-model="dataForm.propType">
          <el-option
            v-for="option in propTypeOptions"
            :key="option.id"
            :label="option.name"
            :value="option.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物品图片：" prop="img">
        <el-upload
          ref="uploadImg"
          :action="uploadImgUrl"
          :on-success="handleImgSuccess"
          :on-error="handleImgError"
          :show-file-list="true"
          :before-upload="beforeImgUpload">
          <img v-if="dataForm.img" :src="dataForm.img" class="img">
          <i v-else class="el-icon-plus img-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="物品描述：" prop="desc">
        <el-input v-model="dataForm.desc" placeholder="" type="textarea" :rows="5" style="width: 400px;"></el-input>
      </el-form-item>
      <el-form-item label="上下架:" prop="status">
        <el-radio v-model="dataForm.status" :label="1">上架</el-radio>
        <el-radio v-model="dataForm.status" :label="0">下架</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        uploadImgUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        dataForm: {
          tag: '',
          name: '',
          img: '',
          propType: '',
          desc: '',
          status: 1
        },
        propTypeOptions: [],
        tagOptions: [],
        dataRule: {
          tag: [
            {required: true, message: '宠物物品tag不能为空', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '物品名称不能为空', trigger: 'blur'}
          ],
          propType: [
            {required: true, message: '物品分类不能为空', trigger: 'blur'}
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getPetPropType()
          this.getPetPropTag()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbpetprop/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                  this.dataForm = data.data.pbPetProp
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbpetprop/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbPetProp: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleImgSuccess (res, file) {
        this.dataForm.img = res.url
        let _this = this
        setTimeout(function () {
          _this.$refs.uploadImg.clearFiles()
        }, 1000)
      },
      beforeImgUpload (file) {
        if (file.type.indexOf('image') < 0) {
          this.$message.warning('图片格式不正确')
          return false
        }
      },
      handleImgError (err, file, fileList) {
        this.$message.error('上传失败')
      },
      getPetPropType () {
        this.$http({
          url: this.$http.adornUrl(`/picbook/combobox/listPetPropType`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.propTypeOptions = data.data
          }
        })
      },
      getPetPropTag () {
        this.$http({
          url: this.$http.adornUrl(`/picbook/combobox/listPetPropTag`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tagOptions = data.data
          }
        })
      },
    }
  }
</script>
<style scoped lang="less">
  .img-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    width: 150px;
    height: 125px;
    line-height: 125px;
    text-align: center;
  }

  .img {
    width: 150px;
    height: 125px;
    display: block;
  }
</style>
